package com.spukmk3me.sound;

import java.util.LinkedList;
import java.util.List;

/**
 *  Wrapper for sound engine
 */
public abstract class SoundPlayer
{
    protected SoundPlayer()
    {
        m_soundIns = new LinkedList<SoundInstance>();
    }
    
    public final SoundInstance createSoundInstance( Sound sound, float volume )
    {
        SoundInstance sndInstance = _createInstance( sound );
        
        sndInstance.setVolume( volume );
        addSoundInstance( sndInstance );
        
        return sndInstance;
    }
    
    private void addSoundInstance( SoundInstance soundInstance )
    {
        if ( soundInstance != null )
        {
            m_soundIns.add( soundInstance );
            soundInstance.getOriginalSound().inc();
        }
    }
    
    public final void clearSounds()
    {
        for ( int i = 0; i != m_soundIns.size(); ++i )
            m_soundIns.get( i ).getOriginalSound().dec();
        
        m_soundIns.clear();
    }
    
    protected abstract SoundInstance _createInstance( Sound sound );
    
    private List<SoundInstance> m_soundIns;
}
